// 引入配置文件
import config from "./config.js";
export default {
	config: {
		baseUrl: config.webUrl,
		staticUrl: config.staticUrl,
		header: {
			'Content-Type': 'application/json;charset=UTF-8',
			'Content-Type': 'application/x-www-form-urlencoded',
			'reg-source': 'mini',
			'app-ver': '2.0.0',
			'platform': 'mini'
		},
		data: {},
		method: "GET",
		dataType: "json",
		complete: function() {

		},
		success() {},
		fail() {}
	},
	// formData(obj){
	// 	transformStr3(str) {
	// 		var re = /_(\w)/g;
	// 		return str.replace(re, function($0, $1) {
	// 			return $1.toUpperCase();
	// 		})
	// 	}
	// 	for(let key in obj){
	// 		let newKey = transformStr3(key);
	// 		delete obj[key];
	// 		obj[new]
	// 	}
	// },
	request(opt = {}) {
		let {
			header,
			method,
			dataType,
			url,
			checkInfo,
			data,
			complete,
			success,
			fail
		} = opt;
		let options = {};
		options.header = header || this.config.header;
		options.method = method || this.config.method;
		options.dataType = dataType || this.config.dataType;
		options.url = this.config.baseUrl + url;
		options.data = data || this.config.data;
		options.complete = complete || this.config.complete;
		options.success = success || this.config.success;
		options.fail = fail || this.config.fail

		// 校驗授權
		// this.checkAuth(checkInfo)
		this.interceptor = {
			request: null,
			response: (response) => {
				return response.data
			}
		}
		return new Promise((resolve, reject) => {
			if (this.interceptor.request) {
				this.interceptor.request(options)
			}

			// 统一的请求日志记录
			// _reqlog(_config)
			options.complete = (response) => {
				let statusCode = response.statusCode
				response.config = options
				if (this.interceptor.response) {
					let newResponse = this.interceptor.response(response)
					if (newResponse) {
						response = newResponse
					}
				}
				// 统一的响应日志记录
				// _reslog(response)
				if (statusCode === 200) { //成功
					resolve(response);
				} else {
					reject(response)
				}
			}
			uni.request(options)
		});

	},
	get(url, data, options = {}) {
		options.url = url;
		options.data = data;
		options.method = 'GET';
		return this.request(options);
	},
	post(url, data, options = {}) {
		options.url = url;
		options.data = data;
		options.method = 'POST';
		return this.request(options);
	},
	// 上传图片
	upload(url, options = {}) {
		options.url = this.config.staticUrl + url;
		options.header = options.header || this.config.header;
		options.fileType = options.fileType || "image";
		options.formData = options.formData || {};
		options.filePath = options.filePath;
		options.name = options.name;
		// TODO：token增加等操作
		if (options.token) {
			// 验证是否登录
			if (!this.checkToken(options.checkToken)) return;
			// 验证权限
			if (!this.checkAuth(options.checkAuth)) return;
			// options.header.token = User.token;
		}

		return uni.uploadFile(options);
	},
	// 错误处理
	errorCheck(err, res, errfun = false, resfun = false) {
		if (err) {
			typeof errfun === 'function' && errfun();
			uni.showToast({
				title: '加载失败',
				icon: "none"
			});
			return false;
		}
		if (res.data.errorCode) {
			typeof errfun === 'function' && resfun();
			uni.showToast({
				title: res.data.msg,
				icon: "none"
			});
			return false;
		}
		return true;
	}

}
